<?php

/**
* @Author:  	Daniel Dinh
* @Email:   	webdevelopmentvn@gmail.com
* @Link: 		http://www.joomsoft.net
* @Version: 	1.0
* @copyright:	Joomsoft.net
* @license:		GNU GPL v3
*/

// No direct access to this file
defined('_JEXEC') or die;

// import Joomla modellist library
jimport('joomla.application.component.modellist');
   
class DaniProModelCategory extends JModelList
{
	/**
	 * Constructor.
	 *
	 * @param	array	An optional associative array of configuration settings.
	 * @see		JController
	 * @since	1.6
	 */
	public function __construct($config = array())
	{
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'id', 'a.id',
				'title','a.title',
				'description','a.description',
				'ordering', 'a.ordering',
				'published', 'a.published',
				'created', 'a.created',
			);
		}
		parent::__construct($config);
	}

		
	protected function getListQuery()
	{
		// Create a new query object.
		$db		= $this->getDbo();
		$query	= $db->getQuery(true);
		
		$query->select('a.*,c.title as categorytitle, s.title as suppliertitle,co.name as countryname');
		$query->from('#__dani_products a ');
		$query->join('left',' #__dani_categories c ON a.category = c.id');
		$query->join('left',' #__dani_supplier s ON a.supplier = s.id');
		$query->join('left',' #__dani_countries co ON a.madein = co.id');
		$orderCol	= $this->state->get('list.ordering', 'a.title');
		$orderDirn	= $this->state->get('list.direction', 'ASC');
		$category = JRequest::getVar('cid');		
		if($category)
			$query->where('a.category='.$category);
		
		$query->where('a.published=1');
		$query->order($db->escape($orderCol.' '.$orderDirn));
		return $query;
	}


	public function getCategory()
	{
		$db = & JFactory::getDBO();
		$db->setQuery("select * from #__dani_categories where id=".JRequest::getVar('cid',0));
		return $db->loadObject();
	}	
}
